#!/bin/env python
# -*- coding: utf-8 -*-

import httplib, urllib
import StringIO, gzip

class HttpFetcher:
	def __init__(self, debug = 0):
		"""
		
		"""
		httplib.HTTPConnection.debuglevel = debug
		self.conn = None
		self.resp = None
		
	def connect(self, server, port=80, ssl=False):
		"""
		set up connection to server
		"""
		if ssl:
			self.conn = httplib.HTTPSConnection(server, port)
		else:
			self.conn = httplib.HTTPConnection(server, port)
		
	def fetch(self, url, headers, data=None, cookie=None):
		"""
		fetch and parser http response
		"""
		if data != None:
			req = 'POST'
		else:
			req = 'GET'
		
		if "Accept-Encoding" in headers:
			skip_accept_encoding = True
		else:
			skip_accept_encoding = False
		self.conn.putrequest(req, url, skip_accept_encoding=skip_accept_encoding)
		
		for key, value in headers.iteritems():
			self.conn.putheader(key, value)
		if data != None:
			self.conn.putheader('Content-Length', len(data))
		if cookie != None:
			self.conn.putheader('Cookie', cookie)
		self.conn.endheaders()
		
		if data != None:
			self.conn.send(data)

		self.resp = self.conn.getresponse()

		if self.resp.getheader('Content-Encoding') == "gzip":
			compresseddata = self.resp.read()
			compressedstream = StringIO.StringIO(compresseddata)
			gzipper = gzip.GzipFile(fileobj=compressedstream)
			content = gzipper.read()
		else:
			content = self.resp.read()

		return content
